﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCSLibrary;
using Utils;
using Utils.DC.Web.UI;
using Utils.DatabaseUtils;

public partial class Admin_Form_Weblink_Add : System.Web.UI.Page
{
    WebLinkUI objContact = new WebLinkUI();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }


    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        if (txtContactID.Value.Trim() == "")
        {
            try
            {
                int result = objContact.Add( txtCompanyName.Text.Trim(), txtWeblink.Text.Trim(), Convert.ToInt32(txtViewOrder.Text));

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objContact.Update(Convert.ToInt32(txtContactID.Value.Trim()), txtCompanyName.Text.Trim(), txtWeblink.Text.Trim(), Convert.ToInt32(txtViewOrder.Text)); 
                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-noi-dung/quan-tri-weblink.aspx");
    }

    #region Cac ham tuong tac du lieu

   

    private void LoadEditData(int ContactID)
    {
        DataSet ds = objContact.Get(ContactID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtContactID.Value = row["WeblinkID"].ToString();
            txtCompanyName.Text = row["CompanyName"].ToString();
            txtViewOrder.Text = row["ViewOrder"].ToString();
            txtWeblink.Text = row["Weblink"].ToString();
            btnCreate.Text = "Cập nhật";
        }
    }

    private void ReInsertData()
    {
        //txtContactID.Value = "";
        txtCompanyName.Text = "";
        txtWeblink.Text = "";
        txtViewOrder.Value = 0;
    }

    #endregion
}
